package com.mykolaBlyk.graphexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.app.NavUtils;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;

public class GraphActivity extends Activity {
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		double[] array=getIntent().getDoubleArrayExtra("points");
		setContentView(new DrawView(this, array));
		// Show the Up button in the action bar.
		setupActionBar();
	}

	class DrawView extends View {
		private Paint paint;
		private Path path;
		private	double[] array;
		private Points points;
		
		public DrawView(Context context, double[] array) {
	      super(context);
	      this.array=array;
	      init();
	    }
	    
	    private void calculate(){
		     Equation eq= new Equation(array[0], array[1], array[2]);
		     this.points=eq.calculateGraph();
		     double widthLeftBoundary= this.getWidth()*0.1;
		     double widthRightBoundary=this.getWidth()*0.9;
		     double heightTopBoundary=this.getHeight()*0.9;
		     double heightBottonBoundary=this.getHeight()*0.1;
		     this.moving();  
	    }
	   
	    private void moving(){
	    	double centerX= (this.getWidth()/2 ); 
		    double centerY= (this.getHeight()/2);
		    
	    	MyPoint start= new MyPoint((points.getStart().getX()+centerX), (-points.getStart().getY()+centerY));
	    	MyPoint c1=new MyPoint((points.getC1().getX()+centerX),  ( -points.getC1().getY()+centerY));
	    	MyPoint c2=new MyPoint( ( points.getC2().getX()+centerX), (-points.getC2().getY()+centerY));
	    	MyPoint end=new MyPoint( (points.getEnd().getX()+centerX), (-points.getEnd().getY()+centerY));
	    	MyPoint leftRoot=new MyPoint( (points.getLeftRoot().getX()+centerX), (-points.getLeftRoot().getY()+centerY));
	    	MyPoint rightRoot=new MyPoint( (points.getRightRoot().getX()+centerX), (-points.getRightRoot().getY()+centerY));
	    		    	
	    	points.setStart(start);
	    	points.setC1(c1);
	    	points.setC2(c2);
	    	points.setEnd(end);
	    	points.setLeftRoot(leftRoot);
	    	points.setRightRoot(rightRoot);
	    }
	    
		@Override
	    protected void onDraw(Canvas canvas) {
		  canvas.drawColor(Color.GREEN);
	      super.onDraw(canvas);
	      paint.setAntiAlias(true);
	      paint.setColor(Color.BLUE);
	      paint.setStrokeWidth(3);
	      this.calculate();
	      path.moveTo((float) this.points.getStart().getX(), (float)points.getStart().getY());
	      path.quadTo((float)points.getC1().getX(), (float)points.getC1().getY(), (float)points.getEnd().getX(),(float) points.getEnd().getY());
	      canvas.drawPath(path, paint);
	      path.reset();
	      
	      path.moveTo((float)this.points.getLeftRoot().getX(), (float)this.points.getLeftRoot().getY());
	      path.quadTo((float)(points.getC2().getX()), (float) (points.getC2().getY()),(float)(points.getRightRoot().getX()), (float)points.getRightRoot().getY());
	      path.lineTo((float)this.points.getLeftRoot().getX(), (float)this.points.getLeftRoot().getY());
	      path.setFillType(Path.FillType.EVEN_ODD);
	      paint.setColor(Color.RED);
	      paint.setAntiAlias(true);
	      paint.setStrokeWidth(1);
	      paint.setStyle(Paint.Style.FILL);
	      canvas.drawPath(path, paint);
		}
		
	    private void init() {
	    	points=new Points();
			paint = new Paint();
			paint.setStyle(Paint.Style.STROKE);
			path = new Path();
		}
	  }
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.graph, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
